﻿using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Forms;
using QuanLyThuPhiTaiLieu.Forms.QuanTriHeThong;
using QuanLyThuPhiTaiLieu.Entity;
using QuanLyThuPhiTaiLieu.Biz;

namespace QuanLyThuPhiTaiLieu
{
    public partial class MDI : Form
    {
        #region Global properties

        public NguoiSuDung CurrentUser { get; set; }

        public Guest CurrentGuest { get; set; }

        public string UserStatus
        {
            get { return textUserStatus.Text; }
            set { textUserStatus.Text = value; }
        }

        public MenuStrip TopMenu
        {
            get { return menuStrip1; }
        }

        public ToolStripStatusLabel Status
        {
            get { return toolStripStatusLabel; }
        }
        #endregion

        public MDI()
        {
            InitializeComponent();            
        }

        private void Exit(object sender, EventArgs e)
        {
            Close();
        }

        private void ChangeSize(object sender, EventArgs e)
        {
            var controls = Controls.Find("mainPanel", true);
            if (controls.Length > 0)
            {
                controls[0].Size = new Size(Size.Width - 16, Size.Height);

                int width = (Size.Width / 2) - (radLabel1.Size.Width / 2);
                int height = (Size.Height / 2) - 150;
                radLabel1.Location = new Point(width, height);

                width = Size.Width / 2 - radLabel2.Size.Width / 2;
                radLabel2.Location = new Point(width, Size.Height / 2 - 100);

                width = Size.Width / 2 - radLabel3.Size.Width / 2;
                radLabel3.Location = new Point(width, Size.Height / 2 - 50);                
            }
        }       

        #region Menu Quan tri he thong

        private void ViewNguoiSuDung(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ViewNguoiSuDung).Name))
            {
                mainPanel.Visible = false;

                var form = new ViewNguoiSuDung {MdiParent = this, Dock = DockStyle.Fill};
                form.Show();
            }
        }
        
        #endregion        

        private void ShowLogin()
        {
            if (Utilities.OpenForm(this, typeof(DangNhap).Name))
            {
                var form = new DangNhap {MdiParent = this, Dock = DockStyle.Fill};
                mainPanel.Visible = false;

                form.Show();
            }
        }

        private void FormLoad(object sender, EventArgs e)
        {
            if (CurrentUser == null)
            {
                ShowMenu(false);
                ShowLogin();
            }
        }

        private void Logout(object sender, EventArgs e)
        {
            CurrentUser = null;
            UserStatus = string.Empty;

            ShowMenu(false);
            ShowLogin();
        }

        public void ShowMenu(bool flag)
        {
            menuStrip1.Visible = flag;

            menuQuanLyNguoiDung.Visible = false;
            menuPhanQuyen.Visible = false;
            menuBieuPhi.Visible = false;
            menuThamKhaiTaiLieu.Visible = false;
            menuKhaiThacVaSuDungTaiLieu.Visible = false;
            menuPhieuBanGiao.Visible = false;
            menuDanhMuc.Visible = false;
            menuQuanLyDanhMuc.Visible = false;            
            menuThongKePhieu.Visible = false;
            menuQuanLyGuest.Visible = false;
            menuUpdateGuestRole.Visible = false;
                        
            if (CurrentUser == null && CurrentGuest == null) return;

            string roles = string.Empty;
            if (CurrentUser == null)
            {
                roles = CurrentGuest.Roles;
            }
            else
            {
                roles = CurrentUser.Roles;
            }

            if (CurrentUser != null && string.Equals(Constants.Securities.Administrator, CurrentUser.QuyenTruyCap, StringComparison.InvariantCultureIgnoreCase))
            {
                menuQuanLyNguoiDung.Visible = true;
                menuPhanQuyen.Visible = true;
                menuQuanLyGuest.Visible = true;
                menuUpdateGuestRole.Visible = true;
            }

            string template = "{0};#";          

            if (string.IsNullOrEmpty(roles)) return;

            if (roles.IndexOf(string.Format(template, Constants.Roles.ThamKhaoTaiLieu)) != -1)
            {
                menuBieuPhi.Visible = true;
                menuThamKhaiTaiLieu.Visible = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.KhaiThacVaSuDungTaiLieu)) != -1)
            {
                menuBieuPhi.Visible = true;
                menuKhaiThacVaSuDungTaiLieu.Visible = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.PhieuBanGiao)) != -1)
            {
                menuPhieuBanGiao.Visible = true;
                menuThongKePhieu.Visible = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.DanhMuc)) != -1)
            {
                menuDanhMuc.Visible = true;
                menuQuanLyDanhMuc.Visible = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.DanhMucChiTiet)) != -1)
            {
                menuDanhMuc.Visible = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.DanhMucBoSung)) != -1)
            {
                menuDanhMuc.Visible = true;
            }

            if (CurrentGuest != null)
            {
                menuThongKePhieu.Visible = false;
                menuThongKeTaiLieuCungCap.Visible = false;
            }
        }

        private void OpenGuideline(object sender, EventArgs e)
        {
            var filePath = Path.Combine(Application.StartupPath, "Huong dan su dung_Bauxit.pdf");
            Process.Start(filePath);
        }

        private void ViewThamKhaoTaiLieu(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ViewThamKhaoTaiLieu).Name))
            {
                mainPanel.Visible = false;

                var form = new ViewThamKhaoTaiLieu { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }

        private void ViewKhaiThacVaSuDungTaiLieu(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ViewKhaiThacVaSuDungTaiLieu).Name))
            {
                mainPanel.Visible = false;

                var form = new ViewKhaiThacVaSuDungTaiLieu { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }

        private void ViewBangKeKhaiPhiSuDung(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ViewBangKeKhaiPhiSuDungTaiLieu).Name))
            {
                mainPanel.Visible = false;

                var form = new ViewBangKeKhaiPhiSuDungTaiLieu { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }

        private void ViewDanhMuc(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ViewDanhMuc).Name))
            {
                mainPanel.Visible = false;

                var form = new ViewDanhMuc { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }

        private void ViewDanhMucChiTiet(object sender, System.EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ViewDanhMucChiTiet).Name))
            {
                mainPanel.Visible = false;

                var form = new ViewDanhMucChiTiet { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }       

        private void ViewPhanQuyen(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(PhanQuyen).Name))
            {
                mainPanel.Visible = false;

                var form = new PhanQuyen { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }

        private void ViewThongKePhieuBanGiao(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ViewThongKePhieuBanGiao).Name))
            {
                mainPanel.Visible = false;

                var form = new ViewThongKePhieuBanGiao { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }

        private void ViewThongKeTaiLieuCungCap(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ViewThongKeTaiLieuCungCap).Name))
            {
                mainPanel.Visible = false;

                var form = new ViewThongKeTaiLieuCungCap { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }

        private void ViewGuest(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ViewGuest).Name))
            {
                mainPanel.Visible = false;

                var form = new ViewGuest { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }

        private void ChangePassword(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(ChangePassword).Name))
            {
                mainPanel.Visible = false;

                var form = new ChangePassword { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }

        private void UpdateGuestRole(object sender, EventArgs e)
        {
            if (Utilities.OpenForm(this, typeof(UpdateGuestRole).Name))
            {
                mainPanel.Visible = false;

                var form = new UpdateGuestRole { MdiParent = this, Dock = DockStyle.Fill };
                form.Show();
            }
        }
    }
}
